import requests
import re
from util.logger import get_looger

# 高清壁纸分页
# page = 1
# url = f'https://www.hdbz.net/wallpaper/creative/list_{page}.html'
# res = requests.get(url)
# html_content = res.content.decode('utf8')
# items = re.findall(
#     r'<a href="(/wallpaper/\d{6}/\d{6}.html)" target="_blank" title="(.*?)".*?class="item">.*?<img.*?data-original="(.*?)".*?class="lazy" />',
#     html_content, re.S)
# for item in items:
#     print(item)


# bilibili 番剧分页

import requests
from util.headers import get_headers_init_user_agent
from util.logger import get_looger
logger = get_looger("bilibili番剧.log", __file__)

logger.info("开启爬虫")

headers = get_headers_init_user_agent()

page = 1
while True:

    logger.info(f"正在爬取第{page}页")

    url = f'https://api.bilibili.com/pgc/season/index/result?st=1&order=3&season_version=-1&spoken_language_type=-1&area=-1&is_finish=-1&copyright=-1&season_status=-1&season_month=-1&year=-1&style_id=-1&sort=0&page={page}&season_type=1&pagesize=20&type=1'

    res = requests.get(url, headers=headers)

    try:
        if res.status_code == 200:
            json_data = res.json()
            data = json_data['data']
            items = data['list']
            for item in items:
                print(item)

            if data['has_next']:
                page += 1
            else:
                break
        else:
            logger.error(res.status_code)
    except Exception as e:
        logger.error(e)


logger.info("结束爬虫")
